package com.baizhou.honglv.controller;

import com.baizhou.honglv.bean.constant.SysConstant;
import com.baizhou.honglv.bean.enums.BaseError;
import com.baizhou.honglv.bean.exception.SysException;
import com.baizhou.honglv.bean.util.ThreadLocalUtil;
import org.springframework.validation.BindingResult;

/**
 * @author HaiPeng Wang
 * @date 2021/7/17 17:09
 * @Description:
 */
public class BaseController<T> {

    protected T getCurrentUserInfo() {
        T currentUser = (T) ThreadLocalUtil.get(SysConstant.THREAD_LOCAL_KEY_LOGIN_USER);

        if (null == currentUser) {
            throw new SysException(BaseError.NO_USER_INFO_FOUND);
        }

        return currentUser;
    }

    protected void validateParams(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new SysException(BaseError.PARAM_ERROR, bindingResult.getFieldError().getDefaultMessage());
        }
    }
}
